from typing import List


class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        n = len(nums)
        # 双指针
        l, r = 0, n - 1
        while l <= r:
            while l <= r and nums[l] != val:
                l += 1
            while l <= r and nums[r] == val:
                r -= 1
            if l < r:
                nums[l], nums[r] = nums[r], nums[l]
                l += 1
                r -= 1
        return l
